package org.snaker.engine.springboot.configuration;

import org.snaker.engine.Expression;
import org.snaker.engine.SnakerEngine;
import org.snaker.engine.interceptors.SnakerInterceptor;
import org.snaker.engine.core.BasicSnakerEngine;
import org.snaker.engine.service.impl.ManagerService;
import org.snaker.engine.service.impl.OrderService;
import org.snaker.engine.service.impl.ProcessService;
import org.snaker.engine.service.impl.QueryService;
import org.snaker.engine.core.ServiceContext;
import org.snaker.engine.service.impl.TaskService;
import org.snaker.engine.springboot.SprintBootContext;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.List;


@Configuration
public class SnakerAutoConfiguration implements InitializingBean {

    @Resource
    private ApplicationContext context;

    @Bean
    public SnakerEngine snakerEngine(
            ProcessService processService,
            QueryService queryService,
            OrderService orderService,
            TaskService taskService,
            ManagerService managerService,
            List<SnakerInterceptor> interceptors,
            Expression expression
    ) {
        return new BasicSnakerEngine(processService, orderService, taskService, queryService, managerService, interceptors, expression);
    }


    @Override
    public void afterPropertiesSet() {
        //初始化snaker引擎的上下文用于查找注入的bean
        //snaker有自己的bean查找机制
        ServiceContext.setContext(new SprintBootContext(context));
    }
}
